package co.gov.medellin.ssti.sub.web.action;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.beans.factory.annotation.Autowired;

import co.gov.medellin.ssti.sub.common.excepcion.SUBException;
import co.gov.medellin.ssti.sub.model.dao.exception.DAOException;
import co.gov.medellin.ssti.sub.model.service.VflexService;
import co.gov.medellin.ssti.sub.model.vo.VflexVO;

public class GrupoFamiliarAction extends Action {
	@Autowired
	VflexService vflexServiceImpl;

	/**
	 * This is the main action called from the Struts framework.
	 * 
	 * @param mapping
	 *            The ActionMapping used to select this instance.
	 * @param form
	 *            The optional ActionForm bean for this request.
	 * @param request
	 *            The HTTP Request we are processing.
	 * @param response
	 *            The HTTP Response we are processing.
	 * @return
	 * @throws java.io.IOException
	 * @throws javax.servlet.ServletException
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {

		// HttpSession session = request.getSession();
		// UserVO user = (UserVO) session.getAttribute("userInfo");

		Collection<VflexVO> cComunas = null;
		Collection<VflexVO> cBarrios = null;
		Collection<VflexVO> cZonas = null;

		String opcion = request.getParameter("Option");

		try {

			cComunas = vflexServiceImpl.findVflex(VflexService.COMUNA);
			cBarrios = vflexServiceImpl.findVflex(VflexService.BARRIO);
			cZonas = vflexServiceImpl.findVflex(VflexService.ZONA);

			request.setAttribute("ListComunas", cComunas);
			request.setAttribute("ListBarrios", cBarrios);
			request.setAttribute("ListZonas", cZonas);
			if ((Object) opcion != null) {
				if (opcion.equals("zona")) {
					String codeZona = request.getParameter("codeZona");
					cComunas = vflexServiceImpl.findVflex(VflexService.COMUNA,
							codeZona);
					cBarrios = vflexServiceImpl.findVflex(VflexService.BARRIO);
					request.setAttribute("ListComunas", cComunas);
					request.setAttribute("ListBarrios", cBarrios);
					request.setAttribute("ListZonas", cZonas);
					request.setAttribute("zona", codeZona);
				} else if (opcion.equals("comuna")) {
					String codeComuna = request.getParameter("vflComuna");
					String codeZona = request.getParameter("codeZona");
					String zona = "";
					if (codeZona.equals("")) {
						zona = vflexServiceImpl.findCodePadreByCodeHijo(
								new Long(codeComuna)).toString();
						request.setAttribute("zona", zona);
					} else {
						zona = codeZona;
						request.setAttribute("zona", codeZona);
					}
					cComunas = vflexServiceImpl.findVflex(VflexService.COMUNA,
							zona);
					cBarrios = vflexServiceImpl.findVflex(VflexService.BARRIO,
							codeComuna);
					request.setAttribute("comuna", codeComuna);
					request.setAttribute("ListComunas", cComunas);
					request.setAttribute("ListBarrios", cBarrios);
					request.setAttribute("ListZonas", cZonas);

				} else if (opcion.equals("barrio")) {
					// String codeComuna = request.getParameter("vflComuna");
					// String codeZona = request.getParameter("codeZona");
					String codeBarrio = request.getParameter("codeBarrio");
					cBarrios = vflexServiceImpl.findVflex(VflexService.BARRIO);
					request.setAttribute("ListBarrios", cBarrios);
					cComunas = vflexServiceImpl.findVflex(VflexService.COMUNA);
					request.setAttribute("ListComunas", cComunas);
					String zona = "";
					String comuna = "";
					comuna = vflexServiceImpl.findCodePadreByCodeHijo(
							new Long(codeBarrio)).toString();
					request.setAttribute("comuna", comuna);
					if (!comuna.equals(""))
						zona = vflexServiceImpl.findCodePadreByCodeHijo(
								new Long(comuna)).toString();
					request.setAttribute("zona", zona);
					request.setAttribute("barrio", codeBarrio);
				}
			}

		} catch (DAOException e) {
			SUBException.logDebugError("", e);
		}
		return mapping.findForward("success");
	}
}